package com.vonchange.purchase.deed.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vonchange.headb.core.map.HeaMap;
import com.vonchange.headb.core.page.IPage;
import com.vonchange.purchase.deed.bean.Deed;
import com.vonchange.purchase.deed.service.IDeedService;
import com.vonchange.utils.page.PageUtils;
import com.vonchange.utils.web.RequestUtils;
import com.vonchange.utils.web.Result;
@Controller
@RequestMapping("/deed")
public class DeedAction {
	@Resource
	private IDeedService deedService;

	@RequestMapping("/findById")
	@ResponseBody
	public Deed findById(@RequestParam("id")String id){
		return deedService.findById(id);
	}
	@RequestMapping("/findList")
	@ResponseBody
	public List<Deed> findList(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> param =RequestUtils.getQueryParams(request);
		return deedService.findList(new HeaMap().setAll(param));
	}
	@RequestMapping("/findPage")
	@ResponseBody
	public IPage<Deed> findPage(Integer pageNo,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
		IPage<Deed> page=PageUtils.initPage(pageNo, pageSize);
		Map<String, Object> param =RequestUtils.getQueryParams(request);	
		return deedService.findPage(page,new HeaMap().setAll(param).setAddOrder("createTime_desc"));
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Result deleteById(@RequestParam("id")String id){
		int result=deedService.deleteById(id);
		if(result==0){
			return new Result().setStatusFail().setMsg("删除失败");
		}
		return new Result().setMsg("删除成功");
	}
	@RequestMapping("/add")
	@ResponseBody
	public Result add(Deed deed,HttpServletRequest request,HttpServletResponse response){
		if(null==deed){
			return new Result().setStatusFail().setMsg("请传入数据");
		}
		String msg="增加";
		deed=deedService.save(deed);
		return new Result().setResult(deed).setMsg(msg+"成功");
	}   
	@RequestMapping("/update")
	@ResponseBody
	public Result update(Deed deed,HttpServletRequest request,HttpServletResponse response){
		if(null==deed){
			return new Result().setStatusFail().setMsg("请传入数据");
		}
		String msg="修改";
		if(null==deed.getId()){
			return new Result().setStatusFail().setMsg("请输入ID");
		}
		int result=deedService.update(deed);
		if(result<=0){
			return new Result().setStatusFail().setMsg(msg+"未成功");
		}
		return new Result().setResult(deed).setMsg(msg+"成功");
	}     


}
